package reactive;

import com.google.common.base.Throwables;
import org.aceor.funny.common.exception.BusinessRuntimeException;
import org.junit.Test;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;

/**
 * Created by lxue on 16/5/7.
 */
public class Main {

    @Test
    public void testCreate() {
        //绑定事件
        Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("haha");
                if (true) {
                    throw new BusinessRuntimeException("internal");
                }
            }
        })
                .doOnError(throwable -> System.out.println(Throwables.getStackTraceAsString(throwable)));

        //subscribe触发事件,接受回调结果
        observable.subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
//                throw new BusinessRuntimeException("I am an exception");
                System.out.println(s);
            }
        });

    }

    @Test
    public void testJust() {
        Observable.just("haha");
    }
}
